package com.editor.utils;

import com.editor.utils.parser.ValuesForParametersAndConditions;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.util.ArrayList;
import java.util.HashMap;

public class ParamsAndCondsTableModel implements TableModel {
	// Создаем панель на которой разместим, рисунок и 
	// таблицы с начальными услосиями (НУ) и параметрами.
	public static String[] COLUMN_NAMES = {"Название", "Значение"};
	
	public static String current = "Схема замещения линии электропередач";
	//public static Object[] possibilities = {"Схема 1", "Схема 2"};

    //Здесь хранятся данные модели
    private HashMap<Integer, ValuesForParametersAndConditions> data = null;

    //Список слушателей событий
    private ArrayList<TableModelListener> listeners = new ArrayList<TableModelListener>();
    //Здесь константы
    //--------------------------------------------------------
	public static final Object[][] DATA_EMPTY = null;
	
	public static final Object[][] DATA_GENERATOR = {
		{"L_d",	0.537},
		{"L_q",	0.346},
		{"L_ad",0.443},
		{"L_aq",0.252},
		{"L_f",	0.561},
		{"L_g",	0.503},
		{"L_h",	0.275},
		{"r",	0.00669},
		{"r_g",	0.0152},
		{"r_f",	0.000345},
		{"r_h",	0.00663},
		{"T_j",	4330}
	};
	
	public static final Object[][] INIT_CONDITION_GENERATOR = {
		{"Teta", 1},
		{"Omega",1},
		{"i_d",	0.223},
		{"i_q",	0.403},
		{"i_f",	2.512},
		{"i_g",	0},
		{"i_h",	0},
		{"u_f", 0.000867},
		{"T_d",	0.431}
    };
	
	public static final Object[][] DATA_SCHEME = {
		{"L_d",	0.25},
		{"L_q",	0.25},
		{"L_ad",0.2},
		{"L_aq",0.2},
		{"L_f",	0.35},
		{"L_g",	0.33},
		{"L_h",	0.33},
		{"r",	0.0026},
		{"r_g",	0.011},
		{"r_f",	0.00025},
		{"r_h",	0.006},
		{"T_j",	10000},
		{"r_C",	0.0004},
		{"L_C",	0.05}
    };

	public static final Object[][] INIT_CONDITION_SCHEME = {
		{"Teta", 0.1},
		{"Omega", 1},
		{"i_d",	0.847},
		{"i_q",	0.316},
		{"i_f",	6.248},
		{"i_g",	0},
		{"i_h",	0},
		{"u_f",	0.00125},
		{"T_d",	0.394}
	};
	
	public static final Object[][] DATA_TRANSFORM = {
		{"r", 0.00313},
		{"L", 0.102}
	};
	
	public static final Object[][] INIT_CONDITION_NAGR = {
		{"i_d",	0.5934},
		{"i_q",	0.8039}
	};
	
	public static final Object[][] DATA_NAGR = {
		{"r", 0.8},
		{"L", 0.6}
	};
	
	public static final Object[][] INIT_CONDITION_LAP = {
		/*{"i_32d",	0.0135)    },
		{"i_32q",	0.4602)    },
		{"i_21d",	0.0068)},
		{"i_21q",	0.2301)},
		{"i_51d",	-0.0068)},
		{"i_51q",	-0.2301)    }*/
            {"L1", 0.4},
            {"R1", 23.0},
            {"L2", 0.6},
            {"R2", 16.0}
    };

	public static final Object[][] DATA_LAP =     {
            {"r", 0.104},
            {"L", 1.685},
            {"x", 0},
            {"y", 0.1},
            {"z", 0.9},
            {"a", 0},
            {"r_k1", 0},
            {"r_k2", 0}
    };

    public static final Object[][] INIT_CONDITION_LAP2 = {
            /*{"i_32d",	0.0135)    },
		{"i_32q",	0.4602)    },
		{"i_21d",	0.0068)},
		{"i_21q",	0.2301)},
		{"i_51d",	-0.0068)},
		{"i_51q",	-0.2301)    }*/
            {"L1", 0.4},
            {"R1", 23.0},
            {"L2", 0.6},
            {"R2", 16.0}
    };

    public static final Object[][] DATA_LAP2 = {
            {"r", 0.104},
            {"L", 1.685},
            {"x", 0},
            {"y", 0.1},
            {"z", 0.9},
            {"a", 0},
            {"r_k1", 0},
            {"r_k2", 0}
    };

    //-------------------------------------------------
    //Конец констант

    public ParamsAndCondsTableModel(HashMap<Integer, ValuesForParametersAndConditions> newData) {
        this.data = newData;
    }


    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return COLUMN_NAMES[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0)
            return String.class;
        else
            return Double.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 1;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ValuesForParametersAndConditions cell = data.get(rowIndex);
        if (columnIndex == 0)
            return cell.getName();
        else
            return cell.getValue();
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        ValuesForParametersAndConditions newCell = new ValuesForParametersAndConditions(data.get(rowIndex).getName(),(Double) aValue);
        data.put(rowIndex, newCell);
    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        if (listeners.contains(l))
            return;
        listeners.add(l);
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        listeners.remove(l);
    }
}

	
